<script setup>
import { showSuccessToast, showFailToast } from 'vant'
import { ref } from 'vue'
import { sendBindSmsCode } from '@/api/login'
import { useRoute, useRouter } from 'vue-router'
import { bindQrCode } from '@/api/jdb'

const router = useRouter()
const route = useRoute()

const form = ref({
  phone: undefined,
  code: undefined,
})
const sendCodeBtnDisabled = ref(false)
const sendCodeBtnTxt = ref('发送验证码')
const countdownTime = ref(60)
const countdownFlag = ref(0)
const submitLoading = ref(false)

const clearCountdownFlag = () => {
  if (!!countdownFlag.value && countdownFlag.value > 0) {
    clearTimeout(countdownFlag.value)
  }
}
const resetCountdown = () => {
  countdownTime.value = 60
  sendCodeBtnDisabled.value = false
  sendCodeBtnTxt.value = '获取验证码'
  clearCountdownFlag()
}
const countdown = () => {
  if (countdownTime.value <= 0) {
    resetCountdown()
    return
  }
  sendCodeBtnTxt.value = countdownTime.value + '秒后重新获取'
  countdownTime.value--
  sessionStorage.setItem('countdownTime', countdownTime.value)
  setTimeout(countdown, 1000)
}
const startCountdown = () => {
  countdownFlag.value = setTimeout(countdown, 1000)
  sendCodeBtnDisabled.value = true
}
const getCode = () => {
  if(!form.value.phone || form.value.phone.length !== 11 || !(/^1[3456789]\d{9}$/.test(form.value.phone))) {
    showFailToast('手机号格式错误')
    return
  }
  startCountdown()
  sendBindSmsCode(form.value.phone).then(response => {
    showSuccessToast('验证码发送成功')
  })
}

const onSubmit = () => {
  if(!form.value.phone || form.value.phone.length !== 11 || !(/^1[3456789]\d{9}$/.test(form.value.phone))) {
    showFailToast('手机号格式错误')
    return
  }
  // if(!form.value.code || form.value.code.length !== 6) {
  //   showFailToast('验证码错误')
  //   return
  // }
  submitLoading.value = true
  // let redirectTo = decodeURIComponent(route.query.redirect) || '/index'

  bindQrCode({tel: form.value.phone, codeId: route.query.qrCodeId}).then((response) => {
    showSuccessToast('绑定成功')
    router.replace({path: '/success'})
  }).finally(() => submitLoading.value = false)
}

const init = () => {
  // const countdownTimeFromSessionStorage = sessionStorage.getItem('countdownTime')
  // let countdownTimeTotal = 0
  // if (!!countdownTimeFromSessionStorage) {
  //   countdownTimeTotal = parseInt(countdownTimeFromSessionStorage)
  // }
  // if (!isNaN(countdownTimeTotal) && countdownTimeTotal > 0) {
  //   countdownTime.value = countdownTimeTotal
  //   startCountdown()
  // }
}
init()
</script>

<template>
  <header>
    <div class="header">绑定二维码，成为分销商</div>
  </header>
  <van-form @submit="onSubmit" class="form">
    <div class="van-hairline--top"></div>
    <van-cell-group>
      <van-field
          :label-width="'90px'"
          size="large"
          style="background: none;font-size:16px;"
          center
          v-model="form.phone"
          name="手机号"
          placeholder="请输入手机号码"
      >
        <template #label>
          <div style="width: 100%;text-align: right;color:#32434C;font-size:16px;">手机号：</div>
        </template>
      </van-field>
    </van-cell-group>
    <div class="van-hairline--top"></div>
<!--    <van-cell-group>-->
<!--      <van-field-->
<!--          :label-width="'90px'"-->
<!--          size="large"-->
<!--          maxlength="6"-->
<!--          center-->
<!--          style="background: none;font-size:16px;"-->
<!--          v-model="form.code"-->
<!--          name="验证码"-->
<!--          placeholder="请填写验证码"-->
<!--      >-->
<!--        <template #label>-->
<!--          <div style="width: 100%;text-align: right;color:#32434C;font-size:16px;">验证码：</div>-->
<!--        </template>-->
<!--        <template #button>-->
<!--          <van-button-->
<!--              :disabled="sendCodeBtnDisabled"-->
<!--              type="primary"-->
<!--              round-->
<!--              @click.stop.prevent="getCode"-->
<!--              color="#04be02"-->
<!--              style="padding:0;font-size:14px;width:135px;height:35px;border-radius: 5px;margin-right:20px;"-->
<!--          >-->
<!--            {{sendCodeBtnTxt}}-->
<!--          </van-button>-->
<!--        </template>-->
<!--      </van-field>-->
<!--    </van-cell-group>-->
<!--    <div class="van-hairline&#45;&#45;top"></div>-->
    <van-button
        style="margin:80px auto 0;font-size:18px;border-radius: 5px;width:330px"
        block
        type="primary"
        native-type="submit"
        color="#04be02"
        :loading="submitLoading"
        loading-text="加载中..."
    >
      绑 定
    </van-button>
  </van-form>
</template>

<style scoped>
.header {
  text-align: center;
  font-size: 19px;
  color: #3cc51f;
  font-weight: 400;
  margin: 50px auto;
}
:deep(.van-field__control::-webkit-input-placeholder) {
  color: #777;
  font-size: 16px;
}
:deep(.van-field__control::placeholder) {
  color: #777;
  font-size: 16px;
}
:deep(.van-field) {
  padding-left:0;
  padding-right: 0;
}
.form-cell-group :deep(.van-cell) {
  padding-left: 0;
  padding-right: 0;
  margin: 0 auto;
  height: 60px;
}
</style>
